var http = require('http')
var querystring = require('querystring')
var zlib = require('zlib')

var options = {
    hostname:'www.jijidown.com',
    port:'80',
    path:'/Api/Login/',
    method:'POST',
    headers:{
        'Accept':'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Connection':'keep-alive',
        'Content-Type':'application/x-www-form-urlencoded',
        'Host':'www.jijidown.com',
        'Origin':'http://www.jijidown.com',
        'Referer':'http://www.jijidown.com/',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        'X-Requested-With':'XMLHttpRequest',
    }
}
var args = {
    contents: querystring.stringify({
        UserName: 'hanseyvv1992',
        PassWord: '2692290521'
    }),
};

var get = function(options, args, callback){
    var req = http.request(options, function(res){
        var chunks =[]
        var data
        var encoding = res.headers['content-encoding']
        console.log(JSON.stringify(res.headers));

        if(encoding === 'undefined'){
            res.setEncoding('utf-8')
        }
        res.on('data', function (chunk) {
            chunks.push(chunk)
        })
        res.on('end', function(){
            var buffer = Buffer.concat(chunks);
            if(encoding == 'gzip'){
                zlib.gunzip(buffer, function (err, decoded) {
                data = decoded.toString();
                callback(err, args, res.headers, data);
                });
            }else if (encoding == 'deflate') {
                zlib.inflate(buffer, function (err, decoded) {
                    data = decoded.toString();
                    callback(err, args, res.headers, data);
                });
            } else {
                data = buffer.toString();
                callback(null, args, res.headers, data);
            }
        })
    })
    req.on('error',function(e){
        onsole.log('Error:'+e.message)
    })
    req.write(args.contents)
    req.end()
}
get(options,args, function(err,args, headers, data){
    console.log('==>header \n', headers)
    console.log('==>data \n', data)
    var regExp =/"Data":"\S+"/g
    var str = regExp.exec(data)
    var token = str[0].substring(8,40)
    console.log('==>token \n',token)
})




    